package cc.linker.steplink.crm.merchanth5.service.redis;

import cc.linker.steplink.crm.merchanth5.pojo.coupon.CustomerCouponVo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author: CKQ
 * @DateTime: 2020/12/18 8:39
 * @Description: TODO
 */
@Slf4j
@Service
public class CouponCacheService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public final String YZJ_COUPONS = "yzj_coupons:";

    /**
     * @Description：设置雅智捷核销券到缓存
     * @DateTime：2020/12/1 17:23
     * @Params：
     * @Return：
     */
    public void setYzjCouponsToCache(List<CustomerCouponVo> customerCouponVoList) {
        for (CustomerCouponVo customerCouponVo : customerCouponVoList) {
            this.stringRedisTemplate.opsForValue().set(YZJ_COUPONS + customerCouponVo.getConsumptionId(), JSON.toJSONString(customerCouponVo, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue));
        }
    }

    /**
     * @Description：通过核销券删除雅智捷优惠券信息
     * @DateTime：2020/12/18 8:45
     * @Params：
     * @Return：
     */
    public void deleteYzjCouponByConsumptionId(String consumptionId) {
        this.stringRedisTemplate.delete(YZJ_COUPONS + consumptionId);
    }

    /**
     * @Description：通过核销号获取雅智捷的优惠券数据
     * @DateTime：2020/12/18 8:44
     * @Params：
     * @Return：
     */
    public CustomerCouponVo getYzjCouponByConsumptionId(String consumptionId) {
        String couponStr = this.stringRedisTemplate.opsForValue().get(YZJ_COUPONS + consumptionId);
        if (!StringUtils.isEmpty(couponStr)) {
            return JSON.parseObject(couponStr, CustomerCouponVo.class);
        }
        return null;
    }
}
